import axios from 'axios';
import qs from 'qs';
import Vue from 'vue';

const root = process.env.VUE_APP_BASE_API;
const protocol = window.location.protocol; //协议
const host = window.location.host; //主机

if (process.env.NODE_ENV == 'development') {
    axios.defaults.baseURL = root; //接口前缀
} else {
    axios.defaults.baseURL = protocol + '//' + host + "/";  //动态接口配置
}
axios.defaults.withCredentials = true;//跨域
axios.defaults.timeout = 60000; //接口延时

//http request 拦截器
axios.interceptors.request.use(
    config => {
        if (config.method === 'post' && config.data instanceof Array == false) {
            if (config.headers['Content-Type'] != "application/json;charset=UTF-8") {
                config.data = qs.stringify(config.data)
            }
        }
        const Authorization = Vue.prototype.$cookie.get('Authorization');
        if (Authorization) {
            config.headers.Authorization = Authorization;
        }
        return config;
    },
    error => {
        return Promise.reject(err);
    }
);

//响应拦截器即异常处理
axios.interceptors.response.use(response => {
    if (response.data.message && response.data.message.indexOf('尚未登录') != -1) {
        window.location.href = '/#/login';
    }
    return response
}, err => {
    //请求取消时，也会进入error，根据axios.isCancel()：true--请求取消  false--请求失败
    //仅在请求失败时做后续处理
    if (axios.isCancel(err)) {
        console.log('请求取消');
    } else {
        return Promise.resolve(err.response);
    }
});

// 定义对外Get、Post、File请求
export default {
    get(url, param = {}, headers = {}) {
        return AJAX.get(url, {
            params: param,
            headers
        })
    },
    post(url, param = null, headers = {}) {
        param = qs.stringify(param)
        return AJAX.post(url, param, {
            headers
        })
        // application/x-www-form-urlencoded  默认post
    },
    // 针对于后台直接传参格式封装 例如 /{id}
    postParmas(url, paramlink = null, param = null, headers = {}) {
        return AJAX.post(url + '/' + paramlink, param, {
            headers: Object.assign({
                'Content-Type': 'application/json'
            }, headers)
        })
    },
    postJson(url, param = null, headers = {}) {
        return AJAX.post(url, param, {
            headers: Object.assign({
                'Content-Type': 'application/json'
            }, headers)
        })
        // Content-Type: application/json  json
    },
    put(url, param = null, headers = {}) {
        return AJAX.put(url, param, {
            headers
        })
    },
    file(url, param = null, headers = {}) {
        return AJAX.post(url, param, {
            headers: Object.assign({
                'Content-Type': 'multipart/form-data'
            }, headers)
        })
        // Content-Type: multipart/form-data
    },
    delete(url, param = null, headers = {}) {
        return AJAX.delete(url, {
            param,
            headers: Object.assign({
                'Content-Type': 'multipart/form-data'
            }, headers)
        })
    }
}
